Telegram Group & Telegram Channel
📚 Организация middleware в Go без сторонних зависимостей

Если ты пишешь веб-приложения на Go и хочешь избавиться от внешних зависимостей вроде alice, статья от Алекса Эдвардса — must read:
https://www.alexedwards.net/blog/organize-your-go-middleware-without-dependencies

📌 В чем суть:

• Проблема: стандартная библиотека Go не даёт удобного способа цепочечного подключения middleware
• Цель: избежать дублирования кода и сделать middleware масштабируемыми без сторонних пакетов

💡 Решение — создать собственный тип chain, который позволяет «наматывать» middleware на обработчики:


type chain []func(http.Handler) http.Handler

func (c chain) then(h http.Handler) http.Handler {
for i := len(c) - 1; i >= 0; i-- {
h = c[i](h)
}
return h
}

func (c chain) thenFunc(h http.HandlerFunc) http.Handler {
return c.then(h)
}


Теперь ты можешь описывать цепочки middleware так:


mux := http.NewServeMux()

baseChain := chain{requestID, logRequest}
adminChain := append(baseChain, authenticateUser, requireAdminUser)

mux.Handle("GET /", baseChain.thenFunc(home))
mux.Handle("GET /admin", adminChain.thenFunc(showAdminDashboard))


Этот подход:
• Простой
• Без зависимостей
• Легко расширяется

Полная статья и объяснения тут:

@golang_books
Please open Telegram to view this post
VIEW IN TELEGRAM



tg-me.com/golang_books/975
Create:
Last Update:

📚 Организация middleware в Go без сторонних зависимостей

Если ты пишешь веб-приложения на Go и хочешь избавиться от внешних зависимостей вроде alice, статья от Алекса Эдвардса — must read:
https://www.alexedwards.net/blog/organize-your-go-middleware-without-dependencies

📌 В чем суть:

• Проблема: стандартная библиотека Go не даёт удобного способа цепочечного подключения middleware
• Цель: избежать дублирования кода и сделать middleware масштабируемыми без сторонних пакетов

💡 Решение — создать собственный тип chain, который позволяет «наматывать» middleware на обработчики:


type chain []func(http.Handler) http.Handler

func (c chain) then(h http.Handler) http.Handler {
for i := len(c) - 1; i >= 0; i-- {
h = c[i](h)
}
return h
}

func (c chain) thenFunc(h http.HandlerFunc) http.Handler {
return c.then(h)
}


Теперь ты можешь описывать цепочки middleware так:


mux := http.NewServeMux()

baseChain := chain{requestID, logRequest}
adminChain := append(baseChain, authenticateUser, requireAdminUser)

mux.Handle("GET /", baseChain.thenFunc(home))
mux.Handle("GET /admin", adminChain.thenFunc(showAdminDashboard))


Этот подход:
• Простой
• Без зависимостей
• Легко расширяется

Полная статья и объяснения тут:

@golang_books

BY Golang Books


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/golang_books/975

View MORE
Open in Telegram


Golang Books Telegram | DID YOU KNOW?

Date: |

China’s stock markets are some of the largest in the world, with total market capitalization reaching RMB 79 trillion (US$12.2 trillion) in 2020. China’s stock markets are seen as a crucial tool for driving economic growth, in particular for financing the country’s rapidly growing high-tech sectors.Although traditionally closed off to overseas investors, China’s financial markets have gradually been loosening restrictions over the past couple of decades. At the same time, reforms have sought to make it easier for Chinese companies to list on onshore stock exchanges, and new programs have been launched in attempts to lure some of China’s most coveted overseas-listed companies back to the country.

The S&P 500 slumped 1.8% on Monday and Tuesday, thanks to China Evergrande, the Chinese property company that looks like it is ready to default on its more-than $300 billion in debt. Cries of the next Lehman Brothers—or maybe the next Silverado?—echoed through the canyons of Wall Street as investors prepared for the worst.

Golang Books from ca


Telegram Golang Books
FROM USA